package be.fedict.eid.diagnostics.system.windows.proxies;

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.BaseTSD.ULONG_PTR;
import com.sun.jna.platform.win32.WinDef.HMODULE;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.ptr.IntByReference;

public interface Kernel32 extends com.sun.jna.platform.win32.Kernel32 {

	Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
	int PROCESS_QUERY_INFORMATION = 1024;
	int PROCESS_VM_READ = 16;
	
	
	public static class LPPROCESSENTRY32 extends Structure {
		public int     		dwSize;
		public int     		cntUsage;
		public int     		th32ProcessID;
		public ULONG_PTR 	th32DefaultHeapID;
		public int     		th32ModuleID;
		public int     		cntThreads;
		public int     		th32ParentProcessID;
		public NativeLong   pcPriClassBase;
		public int     		dwFlags;
		public char[]      	szExeFile;
		
		public LPPROCESSENTRY32(){
			this.szExeFile = new char[250];
            this.dwSize = this.size();
		}
		
		public String getSzExeFile(){
            StringBuilder sb = new StringBuilder();
            char a, b;
            for (int i = 0; i < szExeFile.length; i++) {
            	a = (char)(szExeFile[i] & 0xFF);
                if (a == 0) 
                	break;
                sb.append(a);
                    
                b = (char)(szExeFile[i] >> 8 & 0xFF);
                if (b == 0) 
                	break;
                sb.append(b);
            }
            return sb.toString();
		}
	}	 
	  
	/**
	 * Takes a snapshot of the specified processes, as well as the heaps, modules, 
	 * and threads used by these processes.
	 * 
	 * http://msdn.microsoft.com/en-us/library/ms682489(VS.85).aspx
	 */

	HANDLE CreateToolhelp32Snapshot(
			int th32csSnapprocess,
			int i
	);
	
	/**
	 * Retrieves information about the first process encountered in a system snapshot.
	 * 
	 * http://msdn.microsoft.com/en-us/library/ms684834(VS.85).aspx
	 */
	
	boolean Process32First(
			HANDLE hSnapshot,
			LPPROCESSENTRY32 procEntry
	);
	
	/**
	 * Retrieves information about the next process recorded in a system snapshot.
	 * 
	 * http://msdn.microsoft.com/en-us/library/ms684836(VS.85).aspx
	 */
	boolean Process32Next(
			HANDLE hSnapshot,
			LPPROCESSENTRY32 procEntry
	);

	/**
	 * Closes an open object handle.
	 * 
	 * http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
	 */
	boolean CloseHandle(
			int hObject);
	
	/**
	 * Opens an existing local process object.
	 * 
	 * http://msdn.microsoft.com/en-us/library/ms684320(VS.85).aspx
	 */
	HANDLE OpenProcess(
			int dwDesiredAccess,
			boolean bInheritHandle,
			int dwProcessId);
		
	
	/**
	 * Retrieves a handle for each module in the specified process 
	 * that meets the specified filter criteria.
	 * 
	 * http://msdn.microsoft.com/en-us/library/ms682633(v=VS.85).aspx
	 */
	boolean EnumProcessModulesEx(
			HANDLE hProcess,
			HMODULE lphModule,
			int cb,
			IntByReference lpcbNeeded,
			int dwFilterFlag); 
	
}
